package com.ssm.server.feign.scanner;

import com.ssm.server.feign.annotaion.MyFeignClient;
import com.ssm.server.feign.factory.MyFeignClientFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.util.LinkedHashSet;
import java.util.Set;

public class MyFeignScanner extends ClassPathBeanDefinitionScanner {

    private final  static  Logger log  = LoggerFactory.getLogger(MyFeignScanner.class);

    public MyFeignScanner(BeanDefinitionRegistry registry) {
        super(registry,false);
        super.addIncludeFilter(new AnnotationTypeFilter(MyFeignClient.class));// 可以不写 反正 在reg里面写了
    }

    @Override
    public Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> dbhs = super.doScan(basePackages);
        Set<BeanDefinitionHolder> copy = new LinkedHashSet<>();
        if(dbhs.isEmpty()){
            return dbhs;
        }
        for (BeanDefinitionHolder holder:dbhs){
            GenericBeanDefinition def = (GenericBeanDefinition) holder.getBeanDefinition();
            // 告诉Spring  fiegn工程是哪一个 && 工厂的构制函数参数
            def.getConstructorArgumentValues().addGenericArgumentValue(def.getBeanClassName());
            def.setBeanClass(MyFeignClientFactoryBean.class);
            copy.add(holder);
        }
        return copy;
    }


    @Override
    public boolean isCandidateComponent(AnnotatedBeanDefinition b){
        return b.getMetadata().isInterface() && b.getMetadata().isIndependent() ;
    }

}
